package sma.fbss.agent;

//import java.util.Date;

import sma.fbss.gui.GUIBuyer;
import sma.fbss.model.MDDesPkg;
import sma.fbss.model.MDPurchase;
//import sma.fbss.model.MDFBaller;
import sma.fbss.model.MDSeller;
//import ifi.sma.mss.models.DesPkgModel;
//import sma.fbss.model.MDSeller;
import sma.fbss.model.MDBuyer;

//import com.google.gson.Gson;

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class Buyer extends Agent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GUIBuyer gui;
	private MDPurchase md_purchase;
	private int step = 0;
	private int best_price;
	private int repliesCnt = 0; // The counter of replies from seller agents
	private MessageTemplate mt; // The template to receive replies
	public String msg;
	private DFAgentDescription seller = null;
	public MDBuyer data_model;

	@Override
	protected void setup() {
		super.setup();
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());
		String agent_name = this.getAID().getName();
		this.data_model = new MDBuyer(agent_name);
		gui = new GUIBuyer(this);

		addBehaviour(new SellerConnect());
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType("seller");
		template.addServices(sd);
		DFAgentDescription[] results;
		try {
			results = DFService.search(this, template);
			if (results.length > 0)
				seller = results[0];
			DFService.register(this, dfd);
		} catch (FIPAException e) {
			e.printStackTrace();
		}
	}

	public void setMessage(String msg, int _step) {
		this.msg = msg;
		this.step = _step;
	}

	private class SellerConnect extends CyclicBehaviour {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void action() {
			System.out.println(msg + "ngu thoi" + seller.getName().getName());
			switch (step) {
			case 0:
				// Send the cfp to seller
				if (seller != null) 
				{
					System.out.println(msg + "ngu thoi" + seller.getName());
					ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
					cfp.addReceiver(seller.getName());
					
					cfp.setContent(msg + "hello ");
					cfp.setConversationId("footballer-trade");
					cfp.setReplyWith("cfp" + System.currentTimeMillis());
					myAgent.send(cfp);
					mt = MessageTemplate.and(MessageTemplate.MatchConversationId("footballer-trade"),
							MessageTemplate.MatchInReplyTo(cfp.getReplyWith()));
				}
				step = 1;
				break;
			case 1:
				ACLMessage recMsg = myAgent.receive();
				if (recMsg != null) {
					String content = recMsg.getContent();
					String[] con = content.split("//");
					if(con.length > 4)
					{
						String stat = con[0];
						String ach = con[1];
						String ven = con[2];
						String foot = con[3];
						String price = con[4];
						
						if (stat.equalsIgnoreCase("badpurchase")) {
							gui.receiveMsg("L'équipe vendu n'est pas d'accord");
						}
						else if (stat.equalsIgnoreCase("bestpurchase")) {
							//Codez pour mettre a jour l'achat
							float pri = Float.parseFloat(price);
							md_purchase = new MDPurchase(ven, ach, foot, pri, "");
							md_purchase.setPurchase();
							gui.receiveMsg("Acheter réusi le footballeur " + foot + 
									" de\n Equipe :" + seller.getName() + " - " + ven);
							}
						}
					step = 2;
					}
				else block();
				break;

				case 2:
				break;
			}

		}
		

	}
}
